package com.py.credit.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.py.credit.constant.ResourcePath;
import com.py.credit.entity.Hcbg;
import com.py.credit.service.WebBgdzService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.rest.support.RestServiceResult;

/**
 * 门户网站核查报告定制化Controller
 */

@RestController
@RequestMapping("/webcreditbg")
public class WebBgdzController {
	@Autowired
	private WebBgdzService WebBgdzService;

	@Value("${absolutepath}")
	private String absolutepath;

	@RequestMapping("/addReportElement.json")
	public RestServiceResult<List<Map<String, Object>>> addBgdz(
			@RequestParam(value = "reportNo", required = false) String reportNo,
			@RequestParam(value = "reportProducer", required = false) String reportProducer,
			@RequestParam(value = "reportStatment", required = false) String reportStatment,
			@RequestParam(value = "waterMark", required = false) CommonsMultipartFile waterMark) throws IOException {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();

		String watermark = absolutepath + ResourcePath.WATERMAERK_IMAGES_DIR;
		WebBgdzService.addHcbg(watermark, reportNo, reportStatment, waterMark, reportProducer);
		return result;

	}

	@RequestMapping("/getReportTextElement.json")
	public RestServiceResult<Hcbg> getReportTextElement() {
		RestServiceResult<Hcbg> result = new RestServiceResult<Hcbg>();
		Hcbg latest = WebBgdzService.getLatest();
		result.setData(latest);
		return result;

	}
	@IgnoreAuthority
	@RequestMapping("/getWatermark.png")
	public void getLoginLogo(HttpServletResponse response) throws IOException {
		Hcbg latest = WebBgdzService.getLatest();
		response.setContentType("image/png");
		response.setCharacterEncoding("UTF-8");
		OutputStream outputSream = response.getOutputStream();

		if (new File(absolutepath + latest.getWatermarker()).exists()) {

			InputStream fileInputStream = new FileInputStream(absolutepath + latest.getWatermarker());
			byte[] data = toByteArray(fileInputStream);
			fileInputStream.close();

			InputStream in = new ByteArrayInputStream(data);
			in.close();
			int len = 0;

			byte[] buf = new byte[1024];
			while ((len = in.read(buf, 0, 1024)) != -1) {
				outputSream.write(buf, 0, len);
			}

			outputSream.close();
		}

	}

	private byte[] toByteArray(InputStream in) throws IOException {

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024 * 4];
		int n = 0;
		while ((n = in.read(buffer)) != -1) {
			out.write(buffer, 0, n);
		}
		return out.toByteArray();
	}
}
